package com.spyx.test;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1.创建线程池，内部有3个线程
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        System.out.println("main begin....");
        //2.参数1 是线程的任务内容   参数2 线程池对象
        // CompletableFuture.supplyAsync() 指定让线程做某件事，一定要有返回值
        CompletableFuture<Integer> completableFuture =
                CompletableFuture.supplyAsync(()->{
                    System.out.println("当前线程："+Thread.currentThread());
                    int result = 1024;
                    //System.out.println(5/0);
                    System.out.println("result:"+result);
                    return result;
                },executorService).whenComplete((rs,exception)->{  //rs指的是上面代码返回的结果，exception 上面代码执行后的异常信息
                    System.out.println("rs="+rs);
                    System.out.println("exception="+exception);
                });
        //获取线程的返回结果
        Integer integer = completableFuture.get();
        System.out.println("返回的结果是:"+integer);
        System.out.println("main over....");
    }
}
